package com.bl.weilan.manager.controller.trade;

import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.service.biz.report.ReportService;
import com.bl.weilan.service.biz.trade.TradeService;
import com.bl.weilan.service.biz.tradedetail.TradeDetailService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.companyopponent.CompanyOpponentTradedAddVo;
import com.bl.weilan.service.vo.report.ReportAddVo;
import com.bl.weilan.service.vo.report.ReportListVo;
import com.bl.weilan.service.vo.tradeb.TradeBAddVo;
import com.bl.weilan.service.vo.tradeb.TradeBCListVo;
import com.bl.weilan.service.vo.tradeb.TradeBListVo;
import com.bl.weilan.service.vo.tradec.TradeCAddVo;
import com.bl.weilan.service.vo.tradec.TradeCListVo;
import com.bl.weilan.service.vo.traded.TradeDAddVo;
import com.bl.weilan.service.vo.traded.TradeDListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liangweikang on 19/7/14.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/weilan/manager/trade"})
@Api(value = "行业相关", description = "行业相关")
public class TradeController extends BaseController {
    @Autowired
    TradeService tradeService;

    @Autowired
    TradeDetailService tradeDetailService;

    @ApiOperation(value = "增加四级行业", notes = "增加四级行业 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_traded"})
    public ApiResponseBody add(
            @Valid @RequestBody TradeDAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        tradeService.addTradeD(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "增加二级行业", notes = "增加二级行业 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_tradeb"})
    public ApiResponseBody add(
            @Valid @RequestBody TradeBAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        tradeService.addTradeB(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "增加三级行业", notes = "增加三级行业 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_tradec"})
    public ApiResponseBody add(
            @Valid @RequestBody TradeCAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        tradeService.addTradeC(body);

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "给公司增加四级行业", notes = "给公司增加四级行业 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_cp_traded"})
    public ApiResponseBody add(
            @Valid @RequestBody CompanyOpponentTradedAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        tradeService.addCompanyTradeD(body.getCompanyId(),body.getTradeDId());

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除四级行业", notes = "删除四级行业 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {
        tradeService.del(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除三级行业及其相关信息", notes = "删除三级行业及其相关信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_tradec"})
    public ApiResponseBody delTradeC(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {
        tradeService.delTradeC(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除二级行业及其相关信息", notes = "删除二级行业及其相关信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_tradeb"})
    public ApiResponseBody delTradeB(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {
        tradeService.delTradeB(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "获取三级行业下的四级列表,", notes = "获取三级行业下的四级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeCId", value = "三级行业id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_traded_list"})
    public ApiResponseBody<ReportListVo> reportList(
            @RequestParam(value = "tradeCId", required = true) String tradeCId,
            HttpServletRequest request) {

        TradeDListVo tradeDListVo = tradeService.getTradeDList(tradeCId);

        return ApiResponseBody.createSuccessBody(tradeDListVo);
    }

    @ApiOperation(value = "获取三级行业id,", notes = "获取三级行业id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "三级行业name", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_tradec_id_by_name"})
    public ApiResponseBody<String> getTradeCId(
            @RequestParam(value = "name", required = true) String name,
            HttpServletRequest request) {

        String id = tradeService.getTradeCId(name);

        return ApiResponseBody.createSuccessBody(id);
    }

    @ApiOperation(value = "获取二级行业id,", notes = "获取二级行业id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "三级行业name", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_tradeb_id_by_name"})
    public ApiResponseBody<String> getTradeBId(
            @RequestParam(value = "name", required = true) String name,
            HttpServletRequest request) {

        String id = tradeService.getTradeBId(name);

        return ApiResponseBody.createSuccessBody(id);
    }

    @ApiOperation(value = "获取某个公司下三级行业下的四级列表,", notes = "获取某个公司下三级行业下的四级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "tradeCId", value = "三级行业id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_traded_list_by_company"})
    public ApiResponseBody<ReportListVo> reportList(
            @RequestParam(value = "companyId", required = true) String companyId,
            @RequestParam(value = "tradeCId", required = true) String tradeCId,
            HttpServletRequest request) {

        TradeDListVo tradeDListVo = tradeService.getTradeDListByTradeCAndCompanyId(companyId, tradeCId);

        return ApiResponseBody.createSuccessBody(tradeDListVo);
    }


    @ApiOperation(value = "根据一级行业id获取二三级行业列表", notes = "根据一级行业id获取二三级行业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeaId", value = "tradea_Id", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_tradebc_list"})
    public ApiResponseBody<TradeBCListVo> gettradeBClist(
            @RequestParam(value = "tradeaId", required = true) String tradeaId,
            HttpServletRequest request) {

        TradeBCListVo tradeBListVo = tradeService.getTradeBCList(tradeaId);

        return ApiResponseBody.createSuccessBody(tradeBListVo);
    }

    @ApiOperation(value = "根据一级行业id获取二级行业列表", notes = "根据一级行业id获取二级行业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeaId", value = "tradea_Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_tradeb_list"})
    public ApiResponseBody<TradeBListVo> gettradeBlist(
            @RequestParam(value = "tradeaId", required = true) String tradeaId,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        TradeBListVo tradeBListVo = tradeService.getTradeBList(tradeaId, pageable);

        return ApiResponseBody.createSuccessBody(tradeBListVo);
    }

    @ApiOperation(value = "根据二级行业id获取三级行业列表", notes = "根据二级行业id获取三级行业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradebId", value = "tradeb_Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_tradec_list"})
    public ApiResponseBody<TradeCListVo> gettradeClist(
            @RequestParam(value = "tradebId", required = true) String tradebId,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        TradeCListVo tradeCListVo = tradeService.getTradeCList(tradebId, pageable);

        return ApiResponseBody.createSuccessBody(tradeCListVo);
    }
}
